package Models

import (
	"fmt"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
	metricsv "k8s.io/metrics/pkg/client/clientset/versioned"
	"log"
)

type ClientModel struct{}

// ClientSet客户端
func (c ClientModel) ClientSet() (*kubernetes.Clientset, error) {
	// 1、初始化 config 实例
	config, err := clientcmd.BuildConfigFromFlags("", "./conf/kube.config")
	if err != nil {
		return nil, fmt.Errorf("failed to build config: %w", err)
	}

	// 2、创建客户端工具，clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		return nil, fmt.Errorf("failed to create clientset: %w", err)
	}

	return clientset, err
}

// metricSet客户端
func (c ClientModel) MetricSet() (*metricsv.Clientset, error) {
	config, err := clientcmd.BuildConfigFromFlags("", "./conf/kube.config")
	if err != nil {
		return nil, fmt.Errorf("failed to build config: %w", err)
	}
	metricsClient, err := metricsv.NewForConfig(config)
	if err != nil {
		log.Fatalf("无法创建 Metrics 客户端: %v", err)
	}
	return metricsClient, err
}
